package com.lun.blog.controller.web;

import com.lun.blog.dto.TypeDto;
import com.lun.blog.pojo.Blog;
import com.lun.blog.pojo.Comment;
import com.lun.blog.pojo.Type;
import com.lun.blog.pojo.User;
import com.lun.blog.service.BlogService;
import com.lun.blog.service.CommentService;
import com.lun.blog.service.TypeService;
import com.lun.blog.service.UserService;
import com.lun.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author fangLJ
 * @creatTime 2021/3/7
 */
@Controller
@RequestMapping("/web/type")
public class TypesController {

    @Autowired
    TypeService typeService;

    @Autowired
    BlogService blogService;

    @Autowired
    UserService userService;
    @Autowired
    CommentService commentService;

    @GetMapping("/toType")
    public String toType(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
        List<Map> typeMaps = blogService.findCountByTypeId();
        List<TypeDto> list = new ArrayList<>();
        for (Map typeMap : typeMaps) {
            TypeDto dto = new TypeDto();
            dto.setTypeId(typeMap.get("typeId").toString());
            dto.setTypeName(typeMap.get("typeName").toString());
            dto.setCountBlog(typeMap.get("countBlog").toString());
            list.add(dto);
        }
        model.addAttribute("types",list);
        Page<Blog> blogs = blogService.ListByPublished(pageable);
        List<Blog> content = blogs.getContent();
        Set<Long> userId = content.stream().map(blog -> blog.getUser().getId()).collect(Collectors.toSet());
        Set<Long> typeId = content.stream().map(blog -> blog.getType().getId()).collect(Collectors.toSet());
        List<User> userList = userService.findByUserIds(userId);
        List<Type> typeList = typeService.findByTypeId(typeId);

        for (Blog blog : content) {
            for (User user : userList) {
                if(blog.getUser().getId().equals(user.getId())){
                    blog.setUserName(user.getNickname());
                    break;
                }
            }
            for (Type type : typeList) {
                if(blog.getType().getId().equals(type.getId())){
                    blog.setTypeName(type.getName());
                    break;
                }
            }
            List<Comment> comments = commentService.findByBlogId(blog.getId());
            if(!CollectionUtils.isEmpty(comments)){
               blog.setComment(comments.size());
            }
            blog.setAddress("http://localhost:8080/");
        }
        model.addAttribute("page",blogs);
        return "types";
    }

}
